/**
 * 设备事件移交给上级用户
 */
var experss = require('express');
var router = experss.Router();
var db = require("../public/javascripts/db");
var multer = require('multer');
var upload = multer();
var _ = require("lodash");


router.get('/', function(req, res, next) {
    let eventid = _.trim(String(req.query.eventid));
    // let departid = _.trim(String(req.query.departid));
    let role = _.trim(String(req.query.role));
    let data = {
        isSuccess: false,
        data: null,
        msg: '事件移交失败！'
    };
    // 查询目标责任人的用户id
    let targetRole =  role < 3 ? (Number(role) + 1) : '';
    let sql = "select t1.id from sys_user AS t1 JOIN event_record AS t2 ON t1.departid=t2.departid " 
    +" where t1.role=" + targetRole  + " AND t2.id=" + eventid;
    
    let assistantFiled = targetRole == 2 ? 'assistant' : 'assistant1';
    let passOnDateFiled = targetRole == 2 ? 'passondate' : 'passondate1';

    console.log("sql1: " + sql);
    db.execute(sql, function(err, rows) {
        if(err) {
            return res.json(data);
        } else {
            if(rows[0]) {
                let userid = rows[0].id;
                let sql2 = "UPDATE event_record SET " + assistantFiled + "=" + userid + 
                ", maninchargeid=" + userid + ", " + passOnDateFiled + "='" + new Date().toLocaleDateString() + "' WHERE id =" + eventid;
                console.log("sql2" + sql2)
                db.execute(sql2, function(error, row) {
                    if(error) {
                        return res.json(data);
                    } else {
                        if(row.affectedRows > 0) {
                            data.isSuccess = true;
                            data.msg = "事件移交成功！";
                            return res.json(data);
                        }
                    }
                })
            } else {
                /**
                 * 当前用户为二级用户时
                 * 如果没有二级用户就将数据转到三级用户
                 * 否则返回失败
                 */

                if(targetRole == 2) {
                    targetRole = 3;
                    sql = "select t1.id from sys_user AS t1 JOIN event_record AS t2 ON t1.departid=t2.departid " 
                    +" where t1.role=" + targetRole  + " AND t2.id=" + eventid;
                    console.log(sql);
                    db.execute(sql, function(err, rows) {
                        if(err) {
                            console.log("错误：" + err);
                            return res.json(data);
                        } else {
                            if(rows.length > 0 ) {
                                userid = rows[0].id;
                                let sql3 = "UPDATE event_record SET " + assistantFiled + "=" + userid + 
                                ", maninchargeid=" + userid + ", " + passOnDateFiled + "='" + new Date().toLocaleDateString() + "' WHERE id =" + eventid;
                                console.log("sql2" + sql3)
                                db.execute(sql3, function(error, row) {
                                    if(error) {
                                        return res.json(data);
                                    } else {
                                        if(row.affectedRows > 0) {
                                            data.isSuccess = true;
                                            data.msg = "事件移交成功！";
                                            return res.json(data);
                                        } else {
                                            return res.json(data);
                                        }
                                    }
                                })
                            }
                        }
                    })
                } else {
                    return res.json(data);
                }
            }
        }
    })
});

module.exports = router;